<template>
    <div class="demand-detail">
        <a-modal width="800px" :visible="visible" title="科室信息" @cancel="closeModal" class="yuan-modal demand-detail-modal"
            :dialog-style="{ top: '20px' }">
            <a-spin tip="Loading..." :spinning="saveLoading">
                <a-form-model layout="inline" class="yuan-form" ref="form" :model="formData" :rules="rules"
                    :class="isShow?'disable-form':''" :label-col="{ style: { width: '150px' } }"
                    :wrapper-col="{ style: { width: 'calc(100% - 160px)' } }">

                    <div class="form-panel">
                        <div class="form-panel-box">
                           <a-form-model-item class="yuan-form-item" label="一级科室名称" prop="name">
                                <a-input class="yuan-form-input" placeholder="科室名称" v-model="formData.name"></a-input>
                            </a-form-model-item>
                        </div>
                    </div>
                </a-form-model>
            </a-spin>
            <template slot="footer">
                <template v-if="isShow">
                    <a-button @click="closeModal">
                        关闭
                    </a-button>
                </template>
                <template v-else>
                    <a-button @click="modalOk" type="primary" :loading="saveLoading">
                        确认保存
                    </a-button>
                    <a-button @click="closeModal">
                        取消
                    </a-button>
                </template>

            </template>
        </a-modal>
    </div>
</template>

<script lang="ts">
import { Component, Prop, Watch, Vue } from "vue-property-decorator";
import api from "@/api/api";
import platformApi from "@/api/platformApi";
@Component({
    name: "tpl",
    components: {  },
})
export default class extends Vue {
    @Prop({ default: false }) visible!: boolean;
    @Prop({ default: false }) isShow!: boolean;
    @Prop({ default: "" }) editInfo!: any;

    @Watch("visible")
    visibleChange() {
        if (this.visible) {
            if (this.editInfo.id) {
                this.formData = { ...this.editInfo };
            } 
        } else {
            
        }
    }
    saveLoading = false;
    formData: any = {
        pictures: [],
    };
    rules = {
        name: [{ required: true, message: "请输入科室名称", trigger: "change" }],
    };

    async mounted() {}
    closeModal() {
        this.$emit("closeModal");
    }

    async modalOk() {
        
        (this.$refs.form as any).validate(async (valid: boolean) => {
            if (!valid) {
                this.$message.error("必填信息不完整，请检查后重试");
                return;
            }
            this.saveLoading = true;
            try {
                const res = await platformApi.addOrUpdateBasicsInfo({
                    id:this.formData.id||'',
                    name:this.formData.name,
                    type:'KS',
                    parentId:0
                });
                this.$message.success("保存成功");
                this.closeModal();
                this.$emit("resetList");
            } finally {
                this.saveLoading = false;
            }
        });
    }
}
</script>
<style lang="scss">
.demand-detail-modal {
}
</style>
